package com.guigu.service.impl.stock;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.mapper.stock.SPayMapper;
import com.guigu.pojo.stock.SPay;
import com.guigu.service.stock.SPayService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Mr.Zhang
 * @since 2024-12-06
 */
@Service
public class SPayServiceImpl extends ServiceImpl<SPayMapper, SPay> implements SPayService {

    @Autowired
    private SPayMapper sPayMapper;

    @Override
    public SPay getEndSpay() {
        QueryWrapper<SPay> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("PAY_ID");
        queryWrapper.last("limit 1");
        SPay sPay = getOne(queryWrapper);
        return sPay;
    }

    @Override
    public int addSpay(SPay sPay) {
        //返回主键
        int insert = sPayMapper.insert(sPay);
        return sPay.getId();

    }

    @Override
    public PageInfo<SPay> selectSpayByPage(HashMap<String, Object> map) {
        Integer pageNum = 1;
        Integer pageSize = 5;

        Object pageNumObj = map.get("pageNum");
        if (pageNumObj != null && StringUtils.isNotBlank((String) pageNumObj)) {
            pageNum = Integer.parseInt((String) pageNumObj);
        }
        Object pageSizeObj = map.get("pageSize");
        if (pageSizeObj != null && StringUtils.isNotBlank((String) pageSizeObj)) {
            pageSize = Integer.parseInt((String) pageSizeObj);
        }
        QueryWrapper<SPay> queryWrapper = new QueryWrapper<>();

        if (map.containsKey("checkTag")){
            if(StringUtils.isNotBlank((String) map.get("checkTag"))){
                queryWrapper.eq("CHECK_TAG",map.get("checkTag"));
            }

        }
        if(map.containsKey("storeTag")){
            if(StringUtils.isNotBlank((String) map.get("storeTag"))){
                queryWrapper.eq("STORE_TAG",map.get("storeTag"));
            }
        }
        if(map.containsKey("payCheckTag")){
            if(StringUtils.isNotBlank((String) map.get("payCheckTag"))){
                queryWrapper.eq("PAY_CHECK_TAG",map.get("payCheckTag"));
            }
        }
        if (map.containsKey("keywords")){
            if(StringUtils.isNotBlank((String) map.get("keywords"))){
                queryWrapper.like("REASON",map.get("keywords"));
            }
        }
        if (map.containsKey("payId")){
            if(StringUtils.isNotBlank((String) map.get("payId"))){
                queryWrapper.like("PAY_ID",map.get("payId"));
            }
        }
        if (map.containsKey("startDate") && map.containsKey("endDate")){
            if(StringUtils.isNotBlank(map.get("startDate").toString()) && StringUtils.isNotBlank(map.get("endDate").toString())){
                queryWrapper.between("REGISTER_TIME",map.get("startDate").toString(),map.get("endDate").toString());
            }
        }
        PageHelper.startPage(pageNum,pageSize);
        List<SPay> sPays = sPayMapper.selectList(queryWrapper);
        PageInfo<SPay> pageInfo = new PageInfo<>(sPays);
        return pageInfo;
    }

    @Override
    public boolean updateSpay(SPay spay) {
        QueryWrapper<SPay> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PAY_ID", spay.getPayId());
        int update = sPayMapper.update(spay,queryWrapper);
        return update>0;
    }

    @Override
    public Integer findBySpayId(String spayId) {
        QueryWrapper<SPay> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PAY_ID", spayId);
        SPay sPay = sPayMapper.selectOne(queryWrapper);

        return sPay.getId();
    }
}
